package com.dymall.payment.util;

import com.dymall.payment.domain.dto.CreditCardInfo;
import com.dymall.payment.domain.pojo.CreditCard;
import lombok.RequiredArgsConstructor;
import org.apache.commons.validator.routines.CreditCardValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;

@Component
@RequiredArgsConstructor
public class CreditUtil {

    private final CreditCardValidator creditCardValidator;

    // 模拟信用卡验证方法
    public boolean validateCreditCard(String cardNumber, int cvv, int expirationYear, int expirationMonth) {
        // 验证信用卡是否合法
        if (!creditCardValidator.isValid(cardNumber)) {
            return false;
        }

        if (cvv <= 0 || cvv > 999) {
            return false;  // 简单的CVV验证
        }

        // 验证信用卡的过期日期是否在当前日期之前
        LocalDate currentDate = LocalDate.now();
        LocalDate expirationDate = LocalDate.of(expirationYear, expirationMonth, 1);
        if (expirationDate.isBefore(currentDate)) {
            return false;  // 过期验证
        }

        return true;  // 如果所有验证通过，则认为信用卡有效
    }

    // 比较当前用户的信用卡与请求中的信用卡信息是否匹配
    public boolean isCardMatched(CreditCardInfo userCard, CreditCardInfo providedCard) {
        return userCard.getCardNum().equals(providedCard.getCardNum()) &&
                userCard.getCardCvv().equals(providedCard.getCardCvv()) &&
                userCard.getExpYear().equals(providedCard.getExpYear()) &&
                userCard.getExpMonth().equals(providedCard.getExpMonth());
    }
}
